package com.menu.Service.Impl;

import com.menu.Mapper.MenuMapper;
import com.menu.Mapper.ThumbsUpMapper;
import com.menu.Mapper.UserMapper;
import com.menu.Pojo.Menu;
import com.menu.Pojo.ThumbsUp;
import com.menu.Pojo.User;
import com.menu.Service.NoticeService;
import com.menu.Service.ThumbsUpService;
import com.menu.Utils.ResponseResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ThumbsUpServiceImpl implements ThumbsUpService {
    @Resource
    ThumbsUpMapper thumbsUpMapper;
    @Resource
    UserMapper userMapper;
    @Resource
    MenuMapper menuMapper;
    @Resource
    NoticeService noticeService;

    @Override
    public ResponseResult thumbsUp(Integer userId, Integer menuId) {
        User user= userMapper.findByUserId(userId);
        if(user==null){
            return ResponseResult.error("用户不存在");
        }
        Menu menu= menuMapper.findByMenuId(menuId);
        if (menu == null) {
            return ResponseResult.error("食谱不存在");
        }
        ThumbsUp thumbsUp= thumbsUpMapper.findByUserIdAndMenuId(userId, menuId);
        if(thumbsUp==null){
            thumbsUpMapper.addThumbsUp(userId, menuId);
            Integer i= thumbsUpMapper.countThumbsUp(menuId);
            noticeService.addThumbsUpMenuNotice(menuId,userId,menu.getUserId());
            return ResponseResult.success("点赞成功",i);
        }else{
            thumbsUpMapper.deleteThumbsUp(userId, menuId);
            Integer i= thumbsUpMapper.countThumbsUp(menuId);
            return ResponseResult.success("取消点赞成功",i);
        }

    }

    @Override
    public ResponseResult judgeOfThumbsUp(Integer userId, Integer menuId) {
        User user= userMapper.findByUserId(userId);
        if(user==null){
            return ResponseResult.error("用户不存在");
        }
        Menu menu= menuMapper.findByMenuId(menuId);
        if (menu == null) {
            return ResponseResult.error("食谱不存在");
        }
        ThumbsUp thumbsUp= thumbsUpMapper.findByUserIdAndMenuId(userId, menuId);
        if(thumbsUp==null){
            return ResponseResult.success("没点过赞",0);
        }else{
            return ResponseResult.success("点过赞了",1);
        }
    }
}
